<template>
  <div class="channel-edit-container">
    <!-- 我的频道 -->
    <van-cell center :border="false">
      <div slot="title" class="channel-title">我的频道</div>
      <van-button type="danger" size="mini" round plain class="edit-btn" @click="onEdit"
        >{{isEdit?'完成':'编辑'}}</van-button
      >
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        v-for="(item, i) in myChannels"
        :key="i"
        class="grid-item"
        :class="{'active': item.id === active}"
        :icon="(i !==0 && isEdit) ? 'clear' : ''"
        :text="item.name"
        @click="onMyChannel(item,i)">
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell center :border="false">
      <div slot="title" class="channel-title">频道推荐</div>
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        v-for="(item, i) in recommendchannel"
        :key="i"
        :text="item.name"
        icon="add"
        class="recommend-item"
        @click="onAddChannel(item)"
      />
    </van-grid>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'
import { getAllChannels, addUserChannels, deleteUserChannels } from '@/api/channel'

export default {
  name: 'ChannelEdit',
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [],
      isEdit: false, // 控制关闭按钮的显示于隐藏
      fiexChannel: [0]

    }
  },
  created () {
    this.getAllChannel()
  },
  methods: {
    async getAllChannel () {
      try {
        const { data } = await getAllChannels()
        this.allChannels = data.data.channels
        // console.log(this.myChannels)
      } catch (err) {
        this.$toast.fail('获取所有频道列表失败')
      }
    },
    // 添加频道
    async onAddChannel (item) {
      this.myChannels.push(item)
      if (this.user) {
        // 1,判断状态如果是登陆状态调用接口
        try {
          await addUserChannels({
            id: item.id,
            seq: this.myChannels.length
          })
        } catch (err) {
          this.$toast('添加失败')
        }
      } else {
        // 将数据存储到本地存储
        setItem('TOUTIAO_Channels', this.myChannels)
      }
    },
    // 切换删除频道
    onMyChannel (item, i) {
      if (this.isEdit) {
        // 编辑状态，删除频道
        // 1.如果是推荐不允许删除
        if (this.fiexChannel.includes(item.id)) {
          return
        }
        // 2.如果删除前面的让索引active-1
        if (i < this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        // 3.删除操作
        this.myChannels.splice(i, 1)
        // 调用接口删除
        this.deleteChannel(item)
      } else {
      // 非编辑状态，切换到频道
        this.$emit('update-active', item.id, false)
      }
    },
    async deleteChannel (channelId) {
      if (this.user) {
        try {
          this.$emit('update-active', this.active)
          await deleteUserChannels(channelId.id)
        } catch (err) {
          this.$toast('删除失败')
        }
      } else {
        setItem('TOUTIAO_Channels', this.myChannels)
      }
    },
    onEdit () {
      this.isEdit = !this.isEdit
    }
  },
  computed: {
    ...mapState({
      user: state => state.user.user
    }),
    // 会监测数据的变化
    recommendchannel () {
      return this.allChannels.filter(items => {
        return !this.myChannels.find(item => {
          return item.id === items.id
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.channel-edit-container {
  padding-top: 54px;
  .channel-title {
    font-size: 16px;
    color: #333333;
  }
  .grid-item {
    width: 80px;
    height: 43px;
    /deep/ .van-grid-item__content {
      background-color: #f4f5f6;
      .van-grid-item__text {
        color: #222222;
        font-size: 14px;
        margin-top: 0;
      }
    }
    /deep/ .van-grid-item__icon {
      position: absolute;
      right: -5px;
      top: -5px;
      font-size: 14px;
      color: #cccccc;
    }
  }
  .active {
    /deep/ .van-grid-item__text {
      color: red !important;
    }
  }
  .recommend-item {
    width: 80px;
    height: 43px;
    /deep/ .van-grid-item__content {
      background-color: #f4f5f6;
      .van-grid-item__text {
        color: #222222;
        font-size: 14px;
        margin-top: 0;
      }
      /deep/ .van-grid-item__icon {
        position: absolute;
        right: -5px;
        top: -5px;
        font-size: 14px;
        color: #cccccc;
      }
    }
  }
}
</style>
